package cn.mymanagesys.servlet;

import cn.mymanagesys.entity.SysUser;
import cn.mymanagesys.service.SysUserService;
import cn.mymanagesys.service.impl.SysUserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    private SysUserService sysUserService = new SysUserServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        PrintWriter out = response.getWriter();

        String loginName = request.getParameter("username");
        String password = request.getParameter("password");

        SysUser sysUser = sysUserService.login(loginName, password);

        if (sysUser!=null){
            // 登录成功
            request.getSession().setAttribute("loginUser",sysUser);
            out.print("success");
        } else {
            out.print("error");
        }
    }
}
